/**
 * 
 */
package com.xb.demo1.iterator;

import java.util.ArrayList;
import java.util.List;

/**
 * 书架 (书本集合类)
 * 
 * @author kexue
 *
 */
public class BookShelf {

	private List<Book> books = new ArrayList<Book>();

	/**
	 * 添加书本到书架
	 * 
	 * @param book
	 * @return
	 */
	public boolean add(Book book) {
		return books.add(book);
	}

	/**
	 * 获取当前书架的书本的数量
	 *
	 * @return
	 */
	public int size() {
		return books.size();
	}

	public Iterator<Book> iterator() {

		return new IteratorImpl();
	}

	/**
	 * 内部迭代器
	 * 
	 * @author kexue
	 *
	 */
	class IteratorImpl implements Iterator<Book> {

		private int index = 0;

		public IteratorImpl() {
			index = 0;
		}

		public boolean hasNext() {
			if (BookShelf.this.size() == 0) {
				return false;
			}
			if (index < size()) {
				return true;
			}
			return false;
		}

		public Book next() {
			Book book = books.get(index);
			index++;
			return book;
		}

	}
}
